<?php 
session_cache_expire(999999999);
if(!isset($_SESSION)) 
{ 
	session_start(); 
}
include '../operations/connection.php';	

$groupAssets=$_REQUEST['groupAssets'];
$typeAssets=$_REQUEST['typeAssets'];
$nameAssets=rawurldecode($_REQUEST['nameAssets']);
$countAssets=$_REQUEST['countAssets'];
$notuseAssets=$_REQUEST['notuseAssets'];
$stockAssets=$_REQUEST['stockAssets'];
$valueAssets=str_replace(",", "", $_REQUEST['valueAssets']);
$placeAssets=rawurldecode($_REQUEST['placeAssets']);
$noteAssets=rawurldecode($_REQUEST['noteAssets']);
$memoAssets=rawurldecode($_REQUEST['memoAssets']);


/*$idgroupAssets=0;
$result=mysql_query("SELECT * FROM group_assets g WHERE g.name='".$groupAssets."';");
if (mysql_num_rows($result)>0){
	$r=mysql_fetch_array($result);
	$idgroupAssets=$r['id'];
}else{
	mysql_query("INSERT INTO `group_assets` (`name`) VALUES ('".$groupAssets."')");
	$result=mysql_query("SELECT * FROM group_assets g WHERE g.name='".$groupAssets."';");
	if (mysql_num_rows($result)>0){
		$r=mysql_fetch_array($result);
		$idgroupAssets=$r['id'];
	}
}

$idtypeAssets=0;
$result=mysql_query("SELECT * FROM type_assets t WHERE t.name='".$typeAssets."';");
if (mysql_num_rows($result)>0){
	$r=mysql_fetch_array($result);
	$idtypeAssets=$r['id'];
}else{
	mysql_query("INSERT INTO `type_assets` (`name`) VALUES ('".$typeAssets."')");
	$result=mysql_query("SELECT * FROM type_assets t WHERE t.name='".$typeAssets."';");
	if (mysql_num_rows($result)>0){
		$r=mysql_fetch_array($result);
		$idtypeAssets=$r['id'];
	}
}
*/
$result=mysql_query("SELECT * FROM assets a WHERE a.`name`='".$nameAssets."' AND a.`type`='".$typeAssets."' AND a.`group`='".$groupAssets."';");
$i=0;
while ($r=mysql_fetch_array($result)){
	$i++;
}
$kq=0;
if($i==0){

	if($placeAssets!=""){
		$kq=mysql_query("SELECT * FROM assets_place c WHERE c.`name`='$placeAssets';");
		if(mysql_num_rows($kq)<=0)
			mysql_query("INSERT INTO `assets_place` (`name`) VALUES ('$placeAssets')");
	}
	mysql_query("INSERT INTO assets (`name`,count,stock,notuse,`group`,type,value,place,`note`,`memo`,creater,datetime) VALUES ('".$nameAssets."', '".$countAssets."', '".$stockAssets."', '".$notuseAssets."', '".$groupAssets."','".$typeAssets."','".$valueAssets."', '".$placeAssets."', '".$noteAssets."','".$memoAssets."', '".$_SESSION['IDEmployee']."',NOW())");
}
else {
	$kq=1;
}
echo $kq;
mysql_close($connection);
?>